<?php include "../public/header.php"?>
<?php include "../public/header_public.php"?>
<style>
    .dept_jigou{color: #FF9900;border: 1px solid #FF9900;padding: 2px 20px;border-radius: 5px;background-color: #ff99000d;cursor: pointer;}
    .dept_jigou:hover{color: #FFFFFF;background-color: #FF9900}
    .dept_bumen{color: #19BE6B;border: 1px solid #19BE6B;padding: 2px 20px;border-radius: 5px;background-color: #5fb8781c;cursor: pointer;}
    .dept_bumen:hover{color: #FFFFFF;background-color: #19BE6B}
    .minyuan_red{padding: 4px;background-color: red;border-radius: 50%;font-size: 0;margin-right: 6px;position: relative;top: -4px;}
    .minyuan_lv{padding: 4px;background-color: #19BE6B;border-radius: 50%;font-size: 0;margin-right: 6px;position: relative;top: -4px;}
    .layui-table tr td .laytp-tree-table-cell > .laytp-tree-table-cell-content{
        font-size: 13px;
    }
    #checkallsub{width: 100%;}
</style>
<body class="laytp-container">
<div style="height: 45px"></div>
<div id="opt_nav">
</div>
<div id="dialog">
    <div class="page-title" id="page-title">
        <div class="page-title-con">
            <span class="title">选择预算明细</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button class="layui-btn layui-btn-sm btn-dark marginr5" type="button" id="submit">确定</button>
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
</div>

<div class="layui-card" id="search-form">
    <div class="layui-card-body">
        <form class="layui-form" id="TableSearchFormId" lay-filter="TableSearchForm" onkeydown="keyTableSearch('TableSearchForm', 'datalist')">
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-md3">
                        <label class="layui-form-label">名称及规格</label>
                        <div class="layui-input-block">
                            <input type="hidden" name="subid" id="subid">
                            <input type="text" name="name" placeholder="请输入" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md3">
                        <div class="layui-form-item search-div">
                            <div class="layui-form-item layui-inline">
                                <button class="laytp-btn laytp-btn-md laytp-btn-primary" id="TableSearchButton" type="button"><i class="layui-icon layui-icon-search"></i>查询</button>
                                <button class="layui-btn layui-btn-primary" onclick="listTableReset()" id="TableSearchReset" type="reset" ><i class="layui-icon layui-icon-refresh"></i>重置</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card" id="table-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <div class="layui-col-md2" id="navselect">
                <div class="navselect_bac">
                    <div class="navselect_div">
                        <div class="navselect_title">成本科目</div>
                        <div class="navselect_nav">
                            <a id="tree_pand" title="收起/展开">收起</i></a>&nbsp;
                            <a id="tree_search" title="刷新"><i class="layui-icon layui-icon-refresh-1"></i></a>
                        </div>
                    </div>
                    <div>
                        <button id="checkallsub" type="button" class="layui-btn layui-btn-sm btn-dark">全部</button>
                    </div>
                    <div id="tree_content"></div>
                </div>
            </div>
            <div class="layui-col-md10" id="table_list">
                <div class="layui-card-body">
                    <table id="datalist" lay-filter="datalist"></table>
                </div>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">
    // 单选多选分类 默认单选 choose_type传值为多选
    var choose_type = getQueryVariable('choose_type');
    if(choose_type === undefined || choose_type === false){
        choose_type = 'checkbox';
    }else{
        choose_type = 'radio';
    }

    var parent_dom = getQueryVariable('dom');
    var parent_domkey = getQueryVariable('domkey');
    var parent_budgetid= getQueryVariable('budgetid');
    var parent_page = getQueryVariable('page');

    var mytable, table, form, treeObj, deptTreeData=[],deptTreeStatus=1, navarr;
    layui.use(["laytp",'tree','laypage','table','dropdown','form','soulTable', 'treeTable'], function () {
        var tree = layui.tree,laypage = layui.laypage, table = layui.table, dropdown = layui.dropdown, form = layui.form, soulTable = layui.soulTable, treeTable=layui.treeTable;

        $('#TableSearchReset').click(function () {
            $('.xmselect_div>xm-select').find('.xm-label-block').css('display','none');
            $('.xmselect_div').append('<div class="xmselect_title">请选择</div>');
            searchTableFunc(form, table)
        });

        getDeptData();
        $("#tree_search").click(function () {
            getDeptData();
        });
        $('#tree_pand').click(function () {
            if(deptTreeStatus == 1){
                treeObj.reload({id: 'deptTree',data: opt_tree(deptTreeData, false)});
                deptTreeStatus = 2;
                $("#tree_pand").html('展开');
            }else{
                treeObj.reload({id: 'tree_content',data: opt_tree(deptTreeData, true)});
                deptTreeStatus = 1;
                $("#tree_pand").html('收起');
            }
        });
        $('#checkallsub').click(function () {
            $("#subid").val(0);
            // searchTableList();
            $('.layui-tree-entry').each(function () {
                $(this).removeClass('layui-tree-other');
            });
            searchTableFunc(form, table);
        });
        function getDeptData() {
            facade.ajax({url: ask_engin_budget_getbudsub}).done(function(res){
                if (res.code === 2000) {
                    deptTreeData = res.data;
                    $("#tree_pand").html('收起');
                    treeObj = tree.render({
                        elem: '#tree_content'
                        ,id: 'tree_content'
                        ,data: deptTreeData
                        ,onlyIconControl: true
                        ,click: function(obj){
                            if(obj.data.id && obj.data.id > 0){
                                $("#subid").val(obj.data.id);
                                searchTableFunc(form, table);

                                $('.layui-tree-entry').each(function () {
                                    $(this).find('.layui-tree-txt').removeClass('layui-tree-other');
                                });
                                $(obj.elem).find('.layui-tree-entry').eq(0).find('.layui-tree-txt').addClass('layui-tree-other');
                            }
                        }
                    });
                    var height = $('.laytp-container').height() - 220 + 'px';
                    $('#tree_content').css('cssText','max-height:'+height);
                }
            });
        }


        $("#cols_table").hide();
        $("#export_table").hide();

        mytable = table.render({
            elem: "#datalist",id: "datalist"
            ,url: facade.url(ask_opt_get_engin_budget_list),where: {budgetid:parent_budgetid}, page: false, height: 'full-140'
            ,soulSort: TabInit.soulSort,method: TabInit.method, toolbar: TabInit.toolbar, overflow: TabInit.overflow
            ,defaultToolbar: TabInit.defaultToolbar,filter: TabInit.filter
            ,request: TabInit.request,response: TabInit.response,contextmenu:get_table_contextmenu(soulTable)
            ,text: TabInit.text
            ,parseData: function (res) {
                currButAuth = res.data.butauth;
                tableButShow(currButAuth);

                render_from();
                return {
                    "code": res.code,
                    "msg": res.message,
                    "count": res.data.total,
                    "data": res.data.data
                };
            }
            , cols: [[
                {type: choose_type}
                ,{field: 'name', title: '名称及规格'}
                ,{field: 'unit', title: '单位'}
                ,{field: 'num', title: '预算数量'}
                ,{field: 'price', title: '预算单价(元)'}
                ,{field: 'money', title: '预算金额(元)'}
                ,{field: 'zhannum', title: '占用数量'}
                ,{field: 'shengnum', title: '剩余数量'}
                ,{field: 'explain', title: '备注'}
            ]]
            , done: function (res) {
                soulTable.render(this);
            }
        });
        listTableSearch(form, table);


        $('#submit').on('click', function () {
            var checkStatus = layui.table.checkStatus('datalist');
            var data = checkStatus.data;
            if(data.length < 1){
                facade.error("请选择数据");return false;
            }

            if(parent_page == 'engin_budbian'){
                var str = '';

                var nums = $("#tiao_list tr", parent.document).length;
                for (var key in data){
                    if(data[key].subid){
                        nums++;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td>';
                        str += '<input type="text" listid="'+data[key].id+'" subid="'+data[key].subid+'" value="'+data[key].quanValue+'" disabled placeholder="请选择" class="layui-input subname disabled">';
                        str += '</td>';
                        str += '<td><input type="text" disabled class="layui-input name disabled" value="'+data[key].name+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input unit disabled" value="'+data[key].unit+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input num disabled" value="'+data[key].num+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input price disabled" value="'+data[key].price+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input money disabled" value="'+data[key].money+'"></td>';

                        str += '<td><input type="text" class="layui-input new_num"></td>';
                        str += '<td><input type="text" class="layui-input new_price"></td>';
                        str += '<td><input type="text" disabled class="layui-input new_money disabled" value="0"></td>';
                        str += '<td><input type="text" class="layui-input explain"></td>';
                        str += '</tr>';
                    }
                }
                $("#tiao_list", parent.document).append(str);


                if (typeof(window.parent.update_refresh_row_func)!="undefined"){
                    window.parent.update_refresh_row_func();
                }

            }else if(parent_page == 'contract_cai'){
                var str = '';



                var nums = $("#cai_list tr", parent.document).length;
                for (var key in data){

                    if(data[key].subid){
                        nums++;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td>';
                        str += '<input type="text" onclick="choose_engin_budget_list_one(this)" readonly placeholder="请选择" listid="'+data[key].id+'" value="'+data[key].quanValue+'" class="layui-input subname disabled">';
                        str += '</td>';
                        str += '<td><input type="text" disabled class="layui-input guiname disabled" value="'+data[key].name+'"></td>';
                        str += '<td><input type="text" class="layui-input name" value="'+data[key].name+'"></td>';
                        str += '<td><input type="text" class="layui-input unit" value="'+data[key].unit+'"></td>';
                        str += '<td><input type="text" class="layui-input num"></td>';
                        str += '<td><input type="text" class="layui-input price" value="'+data[key].price+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input money disabled"></td>';
                        str += '<td><input type="text" class="layui-input explain"></td>';
                        str += '</tr>';
                    }
                }
                $("#cai_list", parent.document).append(str);
                if (typeof(window.parent.update_refresh_row_func)!="undefined"){
                    window.parent.update_refresh_row_func();
                }
            }else if(parent_page == 'contract_cai_one'){
                $("#cai_list tr", parent.document).eq(parent_domkey).find('.subname').val(data[0].quanValue).attr('listid',data[0].id);
                $("#cai_list tr", parent.document).eq(parent_domkey).find('.guiname').val(data[0].name);
                $("#cai_list tr", parent.document).eq(parent_domkey).find('.name').val(data[0].name);
                $("#cai_list tr", parent.document).eq(parent_domkey).find('.unit').val(data[0].unit);
                $("#cai_list tr", parent.document).eq(parent_domkey).find('.price').val(data[0].price);

                if (typeof(window.parent.update_refresh_row_func)!="undefined"){
                    window.parent.update_refresh_row_func();
                }
            }else if(parent_page == 'contract_bian'){
                var str = '';
                var nums = $("#list_list tr", parent.document).length;
                for (var key in data){

                    if(data[key].subid){
                        nums++;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td class="layui-hide"><input type="text" onclick="choose_engin_budget_list_one(this)" subid="'+data[key].id+'" value="'+data[key].quanValue+'" class="layui-input subname disabled"></td>';
                        str += '<td class="layui-hide"><input type="text" disabled value="'+data[key].name+'" class="layui-input guiname disabled"></td>';
                        str += '<td><input type="text" class="layui-input name" listid="0" value="'+data[key].name+'"></td>';
                        str += '<td><input type="text" class="layui-input unit" value="'+data[key].unit+'"></td>';
                        str += '<td><input type="text" readonly class="layui-input num_yuan disabled" value="0"></td>';
                        str += '<td><input type="text" readonly class="layui-input price_yuan disabled" value="0"></td>';
                        str += '<td><input type="text" readonly class="layui-input money_yuan disabled" value="0"></td>';
                        str += '<td><input type="number" class="layui-input num_tiao"></td>';
                        str += '<td><input type="number" class="layui-input price_tiao" value="'+data[key].price+'"></td>';
                        str += '<td><input type="number" readonly class="layui-input num disabled"></td>';
                        str += '<td><input type="number" readonly class="layui-input price disabled"></td>';
                        str += '<td><input type="number" readonly class="layui-input money disabled"></td>';
                        str += '<td><input type="text" class="layui-input explain"></td>';
                        str += '<td><input type="text" readonly class="layui-input lai disabled" value="新增"></td>';
                        str += '</tr>';
                    }
                }
                $("#list_list", parent.document).append(str);
                if (typeof(window.parent.update_refresh_row_func)!="undefined"){
                    window.parent.update_refresh_row_func();
                }
            }else if(parent_page == 'contract_bian_one'){
                $("#list_list tr", parent.document).eq(parent_domkey).find('.subname').val(data[0].quanValue).attr('subid',data[0].id);
                $("#list_list tr", parent.document).eq(parent_domkey).find('.guiname').val(data[0].name);

                if (typeof(window.parent.update_refresh_row_func)!="undefined"){
                    window.parent.update_refresh_row_func();
                }
            }else if(parent_page == 'yuwai'){
                $("#list_list tr", parent.document).eq(parent_domkey).find('.subname').val(data[0].topname).attr('listid',data[0].id);
                $("#list_list tr", parent.document).eq(parent_domkey).find('.guiname').val(data[0].name);
                $("#list_list tr", parent.document).eq(parent_domkey).find('.unit').val(data[0].unit);

                if (typeof(window.parent.update_refresh_row_func)!="undefined"){
                    window.parent.update_refresh_row_func();
                }
            }else if(parent_page == 'yuwai_muil'){
                $('#list_list .allxuan', parent.document).each(function (i) {
                    if($(this).prop("checked")){
                        $(this).parents("tr").find('.subname').val(data[0].topname).attr('listid',data[0].id);
                        $(this).parents("tr").find('.guiname').val(data[0].name);
                        // $(this).parents("tr").find('.unit').val(data[0].unit);
                    }
                });
                if (typeof(window.parent.update_refresh_row_func)!="undefined"){
                    window.parent.update_refresh_row_func();
                }
            }else{

            }

            layer_close();
        });
    });



    function chuli_hide_checkbox(data) {
        for(let key in data) {
            if(data[key].children){
                if(data[key].children.length > 0){
                    if(choose_type == 'checkbox'){
                        $('tr[data-id=' + data[key]['id'] + '] .layui-form-checkbox').hide();
                        $('tr[data-id=' + data[key]['id'] + '] input[type="checkbox"]').hide();
                    }else{
                        $('tr[data-id=' + data[key]['id'] + '] .layui-form-radio').hide();
                        $('tr[data-id=' + data[key]['id'] + '] input[type="radio"]').hide();
                    }
                    chuli_hide_checkbox(data[key]['children']);
                }
            }
        }
    }



    function opt_tree(data, status) {
        for (var i=0;i<data.length;i++){
            data[i]['spread'] = status;
            if(data[i].children != undefined){
                opt_tree(data[i].children, status);
            }
        }
        return data;
    }
</script>
</body>
</html>
